package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Search extends JInternalFrame {
	PreparedStatement pstmtTotalScroll, pstmtTotal;

	Connection con;
	private JPanel panel;
	private JButton btnNewButton;
	private JTextField text;
	private JScrollPane scrollPane;
	private JTable table;
	DefaultTableModel model = new DefaultTableModel();
	String[] columnName;
	String[] columnType;
	Object[][] cell_value;
	ResultSet rs;
	ResultSet rsScroll;
	ResultSetMetaData rsmd;
	Update up;
	DatabaseTest d;
	String sql;

	public Search(Connection c, DatabaseTest d) {
		super("search", true, true, true, false);
		this.con = c;
		this.d = d;
		setBounds(150, 150, 340, 234);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);

	}

	// public void search2() {
	// try {
	// model.setColumnCount(0);
	// model.setRowCount(0);
	// String sql = "select * from ptbl order by serial";
	// Statement stmt = con.createStatement();
	// pstmtTotalScroll = con
	// .prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
	// ResultSet.CONCUR_UPDATABLE);
	// pstmtTotal = con.prepareStatement(sql);
	//
	// try {
	// if (sql.toLowerCase().indexOf("select") >= 0) {
	// rsScroll = pstmtTotalScroll.executeQuery();
	// }
	// rs = pstmtTotal.executeQuery();
	//
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }
	// System.out.println("하하");
	// rsmd = rs.getMetaData();
	// int cols = rsmd.getColumnCount();
	// rsScroll.last();
	// String[] columnNam = new String[cols];
	// Vector<String> columnName = new Vector<String>();
	// Vector<String> column = new Vector<String>();
	//
	// for (int i = 0; i < cols; i++) {
	// model.addColumn(rsmd.getColumnName(i + 1));
	// columnNam[i] = rsmd.getColumnName(i + 1);
	// }
	// while (rs.next()) {
	// Vector<String> row = new Vector<String>();
	// for (int i = 0; i < cols; i++) {
	// row.add(rs.getString(i + 1));
	// }
	// model.addRow(row);
	// }
	// rs.close();
	// stmt.close();
	// System.out.println("하하");
	// } catch (Exception e) {
	// System.out.println("예외사항:" + e.getMessage());
	// }
	// }

	// void search() {
	// try {
	//
	// model.setColumnCount(0);
	// model.setRowCount(0);
	// if (text.getText().equals("")) {
	// sql = "select * from ptbl order by no";
	// } else {
	// sql = text.getText();
	// }
	// Statement stmt = con.createStatement();
	// pstmtTotalScroll = con
	// .prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
	// ResultSet.CONCUR_UPDATABLE);
	// pstmtTotal = con.prepareStatement(sql);
	//
	// try {
	// if (sql.toLowerCase().indexOf("select") >= 0) {
	// rsScroll = pstmtTotalScroll.executeQuery();
	// }
	// rs = pstmtTotal.executeQuery();
	//
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }
	//
	// rsmd = rs.getMetaData();
	// int cols = rsmd.getColumnCount();
	// rsScroll.last();
	// String[] columnNam = new String[cols];
	// Vector<String> columnName = new Vector<String>();
	// Vector<String> column = new Vector<String>();
	//
	// for (int i = 0; i < cols; i++) {
	// model.addColumn(rsmd.getColumnName(i + 1));
	// columnNam[i] = rsmd.getColumnName(i + 1);
	// }
	// while (rs.next()) {
	// Vector<String> row = new Vector<String>();
	// for (int i = 0; i < cols; i++) {
	// row.add(rs.getString(i + 1));
	// }
	// model.addRow(row);
	// }
	//
	// rs.close();
	// stmt.close();
	// } catch (Exception e) {
	// System.out.println("예외사항:" + e.getMessage());
	// }
	// }
	public void search() {
		String s = "";
		if (text.getText().equals("")) {
			s = "select * from ptbl order by no";
		} else {
			s = text.getText();
		}
		try {
			PreparedStatement pstmt = con.prepareStatement(s);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Vector v = new Vector<>();
			// v.add("No");
			for (int i = 0; i < rsm.getColumnCount(); i++) {
				v.add(rsm.getColumnName(i + 1));
			}
			model = new DefaultTableModel(v, 0);
			table.setModel(model);

			// int cnt = 1;
			while (rs.next()) {
				Vector d = new Vector<>();
				// d.add(cnt);
				for (int i = 1; i <= rsm.getColumnCount(); i++) {
					// System.out.println(rsm.getColumnCount());
					d.add(rs.getString(i));
				}
				// System.out.println(rsm.getColumnCount());
				model.addRow(d);
				// cnt++;
			}
			rs.close();
			pstmt.close();
		} catch (Exception ex) {
			System.out.println("예외발생");
		}
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.EAST);
			panel.add(getText(), BorderLayout.CENTER);
		}
		return panel;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					search();
				}
			});
		}
		return btnNewButton;
	}

	public void MouseC() {
		int index = table.getSelectedRow();
		Vector<String> vec = new Vector<String>();
		String s = (String) table.getValueAt(index, 0);
		String c = (String) table.getValueAt(index, 1);
		String e = (String) table.getValueAt(index, 2);

		if (d.update == null) {
			d.update = new Update(con, d);
			d.contentPane.add(d.update);

		} else if (d.update.isVisible() == false) {
			d.contentPane.add(d.update);
		}

		d.update.setVisible(true);
		d.update.toFront();
		d.update.setData(s, c, e);

	}

	private JTextField getText() {
		if (text == null) {
			text = new JTextField();
			text.setColumns(10);
		}
		return text;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					MouseC();
				}
			});
		}
		return table;
	}

	public void setD() {
		search();
	}
}